import Request from './request'
import { AxiosResponse } from 'axios'

import type { RequestConfig } from './request'

// 根据项目去修改
export interface ConfigResponse<T> {
    code: number
    msg: string
    data: T
}

// 重写返回类型
interface BaseRequestConfig<T, R> extends RequestConfig<ConfigResponse<R>> {
    data?: T
}

const request = new Request({
    baseURL: import.meta.env.VITE_VUE_BASE_URL,
    timeout: 1000 * 60 * 5,
    interceptors: {
        // 请求拦截器
        requestInterceptors: config => config,
        // 响应拦截器
        responseInterceptors: (result: AxiosResponse) => {
            return result
        },
    },
})

/**
 * @description: 函数的描述
 * @generic D 请求参数
 * @generic T 响应结构
 * @param {BaseRequestConfig} config 不管是GET还是POST请求都使用data
 * @returns {Promise}
 */
const baseRequest = <D = any, T = any>(config: BaseRequestConfig<D, T>) => {
    const { method = 'GET' } = config
    if (method === 'get' || method === 'GET') {
        config.params = config.data
    }
    return request.request<ConfigResponse<T>>(config)
}

export default baseRequest
